// created: 19-6-24
// owner: flwwsg@qq.com
// desc: 解析 yaml 配置

package file

import "io/ioutil"
import "gopkg.in/yaml.v2"

// ReadYamlFromFile 从 yaml 文件中读取配置
func ReadYamlFromFile(filePath string, t interface{}) error {
	f, err := ioutil.ReadFile(filePath)
	if err != nil {
		return err
	}
	err = yaml.Unmarshal(f, t)
	if err != nil {
		return err
	}
	return nil
}

// ReadYamlFromFileByMap 使用 map 解析从 yaml 文件中读取配置
func ReadYamlFromFileByMap(filePath string) (map[string]interface{}, error) {
	m := make(map[string]interface{})
	err := ReadYamlFromFile(filePath, m)
	return m, err
}

// ReadYamlFromString 从字符串中读取配置
func ReadYamlFromString(s string, t interface{}) error {
	return yaml.Unmarshal([]byte(s), t)
}

// ReadYamlFromStringByMap 使用 map 解析
func ReadYamlFromStringByMap(s string) (map[string]interface{}, error) {
	m := make(map[string]interface{})
	err := ReadYamlFromString(s, m)
	return m, err
}
